import request from './request'
import type { ApiResponse, PaginationParams, FilterParams } from '@/types'

/**
 * 产品变体选项值接口
 */
export interface VariantOptionValue {
  option_type: string
  value: string
  color_code?: string
  translations?: Record<string, string>
}

/**
 * 产品变体接口
 */
export interface ProductVariant {
  id?: number
  sku?: string
  option_values: VariantOptionValue[]
  price: number
  stock: number
  product_details?: string
  is_active?: boolean
  sort_order?: number
  images?: Array<{
    id?: number
    url: string
    path?: string
    sort_order?: number
    is_primary?: boolean
  }>
  specifications?: Array<{
    key: string
    value: string
    sort_order?: number
    is_active?: boolean
  }>
}

/**
 * 产品翻译接口
 */
export interface ProductTranslation {
  name: string
  short_desc?: string
  rich_desc?: string
  default_product_details?: string
  general_specifications?: string
}

/**
 * 集成产品接口
 */
export interface ProductIntegrated {
  id?: number
  category_slug?: string  // 兼容旧版
  category_slugs?: string[]  // 支持多选
  category?: {
    id: number
    slug: string
  }
  categories_data?: Array<{
    id: number
    slug: string
    name: string
  }>
  cover_image?: File | string
  hover_image?: File | string
  cover_image_url?: string
  hover_image_url?: string
  is_active?: boolean
  sort_order?: number
  usage?: string
  tag_new?: boolean
  tag_hot?: boolean
  tag_featured?: boolean
  translations: Record<string, ProductTranslation>
  translations_data?: Record<string, ProductTranslation>
  variants: ProductVariant[]
  variants_data?: ProductVariant[]
  created_at?: string
  updated_at?: string
}

/**
 * 分类接口
 */
export interface Category {
  id: number
  slug: string
  name: string
}

/**
 * 选项类型接口
 */
export interface OptionType {
  option_type: string
  display_name: string
}

/**
 * 获取产品列表
 */
export function getProductListIntegrated(params?: PaginationParams & FilterParams) {
  return request<ApiResponse<any>>({
    url: '/products-integrated/',
    method: 'get',
    params
  })
}

/**
 * 获取产品详情
 */
export function getProductIntegrated(id: number) {
  return request<ProductIntegrated>({
    url: `/products-integrated/${id}/`,
    method: 'get'
  })
}

/**
 * 创建产品
 */
export function createProductIntegrated(data: FormData | ProductIntegrated) {
  return request<ProductIntegrated>({
    url: '/products-integrated/',
    method: 'post',
    data,
    headers: data instanceof FormData ? { 'Content-Type': 'multipart/form-data' } : undefined
  })
}

/**
 * 更新产品
 */
export function updateProductIntegrated(id: number, data: FormData | ProductIntegrated | Partial<ProductIntegrated>) {
  return request<ProductIntegrated>({
    url: `/products-integrated/${id}/`,
    method: 'put',
    data,
    headers: data instanceof FormData ? { 'Content-Type': 'multipart/form-data' } : undefined
  })
}

/**
 * 部分更新产品（PATCH）
 */
export function patchProductIntegrated(id: number, data: Partial<ProductIntegrated>) {
  return request<ProductIntegrated>({
    url: `/products-integrated/${id}/`,
    method: 'patch',
    data
  })
}

/**
 * 删除产品
 */
export function deleteProductIntegrated(id: number) {
  return request({
    url: `/products-integrated/${id}/`,
    method: 'delete'
  })
}

/**
 * 批量删除产品
 */
export function bulkDeleteProductsIntegrated(ids: number[]) {
  return request({
    url: '/products-integrated/bulk_delete/',
    method: 'post',
    data: { ids }
  })
}

/**
 * 切换产品激活状态
 */
export function toggleProductActiveIntegrated(id: number) {
  return request<{ is_active: boolean }>({
    url: `/products-integrated/${id}/toggle_active/`,
    method: 'patch'
  })
}

/**
 * 获取分类列表（dog/cat）
 */
export function getCategories() {
  return request<Category[]>({
    url: '/products-integrated/categories/',
    method: 'get'
  })
}

/**
 * 获取可用的选项类型
 */
export function getOptionTypes() {
  return request<OptionType[]>({
    url: '/products-integrated/option_types/',
    method: 'get'
  })
}

/**
 * 上传变体图片
 */
export function uploadVariantImage(file: File) {
  const formData = new FormData()
  formData.append('image', file)
  
  return request<{ url: string; path: string }>({
    url: '/products-integrated/upload-variant-image/',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
